<?php

namespace App\Http\Controllers\Api\V1;

use App\Repository\ThemeRepository;
use Dingo\Api\Exception\StoreResourceFailedException;

/**
 * Class ThemeController
 * @package App\Http\Controllers\Api\V1
 */
class ThemeController extends BaseController
{
    /**
     * @var ThemeRepository
     */
    public $theme;

    /**
     * ThemeController constructor.
     * @param $theme
     */
    public function __construct(ThemeRepository $theme)
    {
        $this->theme = $theme;
    }

    /**
     * 主题相关[这个验证方式不爽 ]
     * @url  /theme?ids=id1,id2,id3....
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function getSimpleList()
    {
        $rules = [
            'ids1' => ['required', 'min:0', 'numeric', 'integer'],
            'ids2' => ['required', 'min:0', 'numeric', 'integer'],
            'ids3' => ['required', 'min:0', 'numeric', 'integer'],
        ];

        $ids = app('request')->only('ids');

        if (empty($ids['ids'])){
            throw new StoreResourceFailedException('Id不能为空');
        }

        $ids = explode(',', $ids['ids']);

        $i = 0;
        foreach ($ids as $key=>$id) {
            $key = 'ids';
            $i += 1;
            unset($ids[0]);
            unset($ids[$i]);
            $ids[$key. $i] = $id;
        }

        $validator = app('validator')->make($ids, $rules);

        if ($validator->fails()) {
            throw new StoreResourceFailedException('ID必须是正整数', $validator->errors());
        }

        $ids = app('request')->only('ids');
        $ids = explode(',', $ids['ids']);

        return  $this->theme->getThemeByIds($ids);

    }

    /**
     * 获取ID的主题下的商品
     * @param $id
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|null|static|static[]
     */
    public function getComplexOne($id)
    {
        $rules = ['id' => 'required|integer|min:0|numeric'];
        $validator = app('validator')->make(['id' => $id], $rules);

        if ($validator->fails()) {
            throw new StoreResourceFailedException('ID必须是正整数', $validator->errors());
        }

        return  $this->theme->getProductsWithTheme($id);

    }
}
